<?php
/**
 *
 * The settings controller manages the site configuration
 *
 * @package    	Digitalus\Core
 * @category 	Controllers
 * @copyright  	Copyright (c) 2009,  Digitalus Media USA (http://digitalusmedia.net)
 * @license    	New BSD License, library/Dig/License.txt
 * @version 	2.0
 *
 */
class Dig_SettingsController extends Zend_Controller_Action
{
    /**
     * This method sets the template to admin for all actions in this controller.
     */
    public function init()
    {
        DigUi_Service_Template::useAdminTemplate();
    }

    /**
     * The index action loads the modules' config files and renders the config file editor.
     */
    public function indexAction()
    {
        $this->view->modules = Dig_Service_Module::listAll();
    }

    /**
     * The save action saves the module's configuration. This form is posted via AJAX
     * so it returns its response as JSON.
     *
     * @param string $id The name of the module
     * @param string $file_contents The module.ini file contents
     * 
     * @return json
     */
    public function saveAction()
    {
        $module = $this->getRequest()->getParam('id');
        $config = $this->getRequest()->getParam('file_contents');
        $modules = Dig_Service_Module::listAll();
        if(isset($modules[$module])) {
            $moduleSettings = $modules[$module];
            $configPath = $moduleSettings['path'] . DIGITALUS_MODULE_CONFIG_FILE;
            // backup file
            file_put_contents($configPath . '.bak', file_get_contents($configPath));
            // now save the changes
            file_put_contents($configPath, $config);
            $response = array(
            	'success' => true,
            	'module' => $module);
        } else {
            $response = array('success' => false);
        }

        return $this->_helper->json($response);
    }

}
?>